﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HFController;

namespace HFWebManage.Source
{
    public partial class Source : HFController.BagePages
    {
      
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断管理员有没有登录，如果没登录，踢出
            this.checkAdminIsLogin();

            //是否首次加载
            if (!Page.IsPostBack)
            {
                //绑定资料库类
                this.bindSourceClass();

                //绑定省份城市
                DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
                this.bindProvinceID();


                //获取Action参数，判断是修改还是删除
                string Action = this.GetQuery("action");

                switch (Action)
                {
                    case "modify":
                        //表示修改
                        this.bindmodel();
                        break;
                    case "delete":
                        //表示删除
                        int ID = int.Parse(this.GetQuery("ID"));
                        LegalAffairsController.getController().doDelegeLegalAffairsBiglClass(ID);
                        break;
                }
            }
        }

        public void bindProvinceID()
        {
            DropProvinceID.DataSource = CityController.getController().getProvinceList(100);
            DropProvinceID.DataValueField = "ID";
            DropProvinceID.DataTextField = "ProvinceName";
            DropProvinceID.DataBind();
            DropProvinceID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        public void bindCityID()
        {
            DropCityID.DataSource = CityController.getController().getCityList(100, DropProvinceID.SelectedValue);
            DropCityID.DataValueField = "ID";
            DropCityID.DataTextField = "CityName";
            DropCityID.DataBind();
            DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 定义一个方法，绑定法律事务
        /// </summary>
        private void bindSourceClass()
        {
            dropSourceID.DataSource = SourceController.getController().getSourceClassList(100);//绑定数据源（下拉框支持List<>类型直接绑定）
            dropSourceID.DataValueField = "ID"; //下拉列表的值，用他写到数据库
            dropSourceID.DataTextField = "ClassName"; //下拉列表的显示文字，和要绑定的数据库字段一致
            dropSourceID.DataBind(); //执行绑定
            dropSourceID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 绑定需要修改的数据
        /// (就是点击修改某条记录，把上面的修改文本的信息，设置成你点的这条记录)
        /// </summary>
        private void bindmodel()
        {
            int ID = int.Parse(this.GetQuery("ID"));
            //得到这条记录
            hf.Model.Source model = SourceController.getController().getSourceModel(ID);
            //输入框赋值
            dropSourceID.SelectedValue = model.SourceID.ToString();
            tbTitle.Text = model.Title;
            tbAuthor.Text = model.Author;
            tbAddtime.Text = model.Addtime.ToString();
            tbReSource.Text = model.ReSource;
            tbContent.Text = model.Content;

            hf.Model.City city = CityController.getController().getCityList().Find(i => i.ID == model.CityID);

            DropProvinceID.SelectedValue = CityController.getController().getProvinceList().Find(i => i.ID == city.ProvinceID).ID.ToString();

            DropCityID.DataSource = CityController.getController().getCityList(100, DropProvinceID.SelectedValue);
            DropCityID.DataValueField = "ID";
            DropCityID.DataTextField = "CityName";
            DropCityID.DataBind();
            DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));

            DropCityID.SelectedValue = model.CityID.ToString();

            //隐藏域ID赋值
            IDS.Value = model.ID.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //获取Action参数，判断是修改还是保存
            string Action = this.GetQuery("action");
            if (Action == "modify")
            {
                //修改记录，刚才bindmodel()方法里，赋值的那两个
                SourceController.getController().doModifySource(int.Parse(IDS.Value),
                    dropSourceID.SelectedValue,
                    tbTitle.Text,
                    tbAuthor.Text,
                    DateTime.Parse(tbAddtime.Text),
                    tbReSource.Text,
                    tbContent.Text,
                    int.Parse(DropCityID.SelectedValue));
            }
            else
            {
                SourceController.getController().doAddSource(dropSourceID.SelectedValue, 
                    tbTitle.Text, 
                    tbAuthor.Text, 
                    DateTime.Parse( tbAddtime.Text),
                    tbReSource.Text, 
                    tbContent.Text, 
                    int.Parse(DropCityID.SelectedValue));
            }
        }

        protected void DropProvinceID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.bindCityID();
        }
    }
}